package com.yueke.gemini.service.impl;

import com.yueke.gemini.common.IReferenceQuery;
import com.yueke.gemini.entity.*;
import com.yueke.gemini.service.IReferenceQueryProvider;
import com.yueke.gemini.service.IReferenceService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2019/11/15.
 */
@Service("referenceService")
public class IReferenceServiceImpl implements IReferenceService {
    private static final Logger logger = LoggerFactory.getLogger(IReferenceServiceImpl.class);

    /**
     * 引用集合
     */
    private List<IReferenceQueryProvider> providers;

    /**
     *
     * @param query
     * @return
     */
    @Override
    public List<KeyItem> getKeyItems(RefQueryDTO query) {
        List<KeyItem> list = null;
        if (StringUtils.isNotBlank(query.getSubModuleCode())) {
            for (IReferenceQueryProvider p : providers) {
                if (StringUtils.isNoneBlank(p.getSubModuleCode()) && p.getSubModuleCode().equals(query.getSubModuleCode())) {
                    list = p.getKeyItems(query);
                }
            }
        }
        return list == null ? new ArrayList<>() : list;
    }

    @Override
    public List<RefModule> getRefModuleList(RefQueryDTO query) {
        List<RefModule> list = null;
        if (StringUtils.isNotBlank(query.getSubModuleCode())) {
            for (IReferenceQueryProvider p : providers) {
                if (StringUtils.isNoneBlank(p.getSubModuleCode()) && p.getSubModuleCode().equals(query.getSubModuleCode())) {
                    String className = p.getClass().getSimpleName();
                    try {
                        list = p.getRefModuleList(query);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return list == null ? new ArrayList<>() : list;
    }

    @Override
    public List<SubRefModule> getSubRefModuleList(RefQueryDTO query) {
        List<SubRefModule> list = null;
        if (StringUtils.isNotBlank(query.getSubModuleCode())) {
            for (IReferenceQueryProvider p : providers) {
                if (StringUtils.isNoneBlank(p.getSubModuleCode()) && p.getSubModuleCode().equals(query.getSubModuleCode())) {
                    String className = p.getClass().getSimpleName();
                    try {
                        list = p.getSubRefModuleList(query);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return list == null ? new ArrayList<>() : list;
    }




    @Override
    public List<ModuleItem> getModuleItemList() {
        List<ModuleItem> items = new ArrayList<>();
        IReferenceQuery.RefModule[] moduleList = IReferenceQuery.RefModule.values();
        IReferenceQuery.SubRefModule[] subModuleList = IReferenceQuery.SubRefModule.values();

        for (IReferenceQuery.RefModule r : moduleList){
            ModuleItem item = new ModuleItem();
            item.setModuleCode(r.getModuleCode());
            item.setModuleName(r.getLabel());

            for (IReferenceQuery.SubRefModule m: subModuleList){
                if (m.getParentModuleCode().equals(item.getModuleCode())){
                    SubModuleItem si = new SubModuleItem();
                    si.setModuleCode(m.getModuleCode());
                    si.setModuleName(m.getLabel());
                    item.getItems().add(si);
                }
            }
            items.add(item);
        }
        return items;
    }

    public List<IReferenceQueryProvider> getProviders() {
        return providers;
    }

    public void setProviders(List<IReferenceQueryProvider> providers) {
        this.providers = providers;
    }
}
